package com.ruiysoft.controller.integralmdse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.controller.integralmdse.util.IntegralMdseUtil;
import com.ruiysoft.controller.integralmdse.vo.InnrIntegralMdseVo;
import com.ruiysoft.controller.mdse.InnrApiController;
import com.ruiysoft.entity.integralmdse.MerchandiseSkuMPRela;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.integralmdse.IMerchandiseSkuMPRelaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-11-03T17:11:33.095+08:00")

@RestController
public class InnrIntegralMdseApiController implements InnrIntegralMdseApi {

    private static final Logger log = LoggerFactory.getLogger(InnrApiController.class);

    private final IMerchandiseSkuMPRelaService merchandiseSkuMPRelaService;

    @org.springframework.beans.factory.annotation.Autowired
    public InnrIntegralMdseApiController(IMerchandiseSkuMPRelaService merchandiseSkuMPRelaService) {
        this.merchandiseSkuMPRelaService = merchandiseSkuMPRelaService;
    }

    @Override
    public Result<Map<Long, InnrIntegralMdseVo>> innrIntegralMdsePost(@Valid List<Long> skuIds, @Valid Integer mLv) {
        try {
            QueryWrapper queryWrapper = new QueryWrapper<MerchandiseSkuMPRela>();
            if(skuIds != null && skuIds.size() > 0) {
                queryWrapper.in("sku_id", skuIds);
                queryWrapper.le("member_level",mLv);
            }

            List<MerchandiseSkuMPRela> bList = merchandiseSkuMPRelaService.list(queryWrapper);
            log.info("ccc------------------>{}",JSONObject.toJSONString(bList));
            Map<Long,InnrIntegralMdseVo> map = bList.stream().collect(Collectors.toMap(a->a.getSkuId(),a->IntegralMdseUtil.BToInnrVo(a),(a,b)->a.getMLv()>b.getMLv()?a:b));

            return ResultGenerator.genSuccessResult(map);

        } catch (Exception e) {
            log.error("innr根据skuId查询商品会员积分价---{}--{}",JSONObject.toJSONString(skuIds),mLv, e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }
}
